﻿using System;
using WorkOrder.Model;

namespace WorkOrder.Business
{
    public class Business_Table_off_day
    {
        DbHelper dbhelper = new DbHelper();

        public bool Insert(Model_Table_off_day model)
        {
            string sqlstr = "insert into Table_off_day (this_day,is_work) values ('" + model.The_day + "'," + (model.Is_work ? 1 : 0).ToString() + ")";
            if (Convert.ToInt32(dbhelper.ExecuteScalar(sqlstr)) == 0)
                return true;
            else
                return false;
        }

        public bool Update(Model_Table_off_day model) 
        {
            string sqlstr = "update Table_off_day set this_day='" + model.The_day + "',is_work=" + (model.Is_work ? 1 : 0).ToString() + " where off_day_id=" + model.Off_day_id;
            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }

        public bool Delete(string id)
        {
            string sqlstr = "delete Table_off_day where off_day_id=" + id;
            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }

        public Model_Table_off_day GetData(string id)
        {
            string sql = "select off_day_id,convert(varchar(20),this_day,23) as this_day,is_work from Table_off_day where off_day_id=" + id;
            using (System.Data.SqlClient.SqlDataReader rdr = dbhelper.Search(sql))
            {
                if (rdr != null && rdr.Read())
                {
                    Model_Table_off_day tmp;
                    tmp = new Model_Table_off_day(rdr["off_day_id"].ToString(), rdr["this_day"].ToString(), !Convert.ToBoolean(rdr["is_work"]));
                    rdr.Close();
                    dbhelper.CloseDbConnection();
                    return tmp;
                }
                else return null;
            }
        }

        public System.Data.DataSet GetDatas(string condition)
        {
            string sql = "select off_day_id,convert(varchar(20),this_day,23) as this_day,is_work from Table_off_day where " + condition + " order by this_day desc";
            return dbhelper.Query(sql);
        }
    }
}
